import tkinter.filedialog
from PIL import Image
import os
from shutil import copyfile

def save_image(file_name, path, name):
    print(path)
    copyfile(file_name, file_name.replace(path, path+'_out'))
    print(name + '\t符合')



def run(ask, size):
    path = tkinter.filedialog.askdirectory()
    print(path)
    for k, t, v in os.walk(path):
        for i in v:
            if '.jpg' in i or '.png' in i or '.JPG' in i:
                file_name = os.path.join(k,i)
                try:
                    img = Image.open(file_name)
                    sizes = size.split('x')
                    wide = int(sizes[0])
                    high = int(sizes[1])
                    if not os.path.exists(os.path.split(file_name.replace(path, path+'_out'))[0]):
                        os.makedirs(os.path.split(file_name.replace(path, path+'_out'))[0])
                    if ask == '1':
                        if img.width < wide and img.height < high:
                            save_image(file_name, path, i)
                    else:
                        if img.width >= wide and img.height >= high:
                            save_image(file_name, path, i)
                except:
                    pass




if __name__ == '__main__':
    print('1、小于尺寸的保留')
    print('2、大于等于尺寸的保留')
    ask = input('输入选项编号：')
    size = input('输入尺寸 宽x高(小写x)：')
    if ask in ['1', '2'] and 'x' in size:
        run(ask, size)


